#ifdef __cplusplus
extern "C" {
#endif
#include "glad/glad.h"
#include "GLFW/glfw3.h"
#ifdef __cplusplus
}
#endif

#include <iostream>

void framebuffer_size_callback(GLFWwindow* win, int32_t width, int32_t height) {
	glViewport(0, 0, width, height);
}

void handle_event(GLFWwindow* win) {
	if (glfwGetKey(win, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
		glfwSetWindowShouldClose(win, true);
	}
}

uint32_t compile_shader(int32_t num, const char* shader_source, int32_t shader_type) {
	uint32_t shader_handle = glCreateShader(shader_type);
	glShaderSource(shader_handle, 1, &shader_source, nullptr);
	glCompileShader(shader_handle);

	int32_t success;
	char log[512];
	glGetShaderiv(shader_handle, GL_COMPILE_STATUS, &success);
	if (!success) {
		glGetShaderInfoLog(shader_handle, 512, nullptr, log);
		std::cout << "COMPILE SHADER ERROR: " << log << " for shader source: " << shader_source << std::endl;
	}

	return shader_handle;
}

const char* VertexShaderSource = "#version 430 core\n"
    "layout(location = 0) in vec3 aPos;\n"
    "void main() {\n"
	"gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0f);\n"
    "}\0";
const char* FragmentShaderSource = "#version 430 core\n"
    "out vec4 FragColor;\n"
	"void main() {\n"
	"    FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
	"}\n\0";

int main() {
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __apple__
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

	GLFWwindow* win = glfwCreateWindow(800, 600, "OpenGL", nullptr, nullptr);
	if (!win) {
		std::cout << "failed to create opengl window." << std::endl;
		glfwTerminate();
		return -1;
	}

	glfwMakeContextCurrent(win);
	glfwSetFramebufferSizeCallback(win, framebuffer_size_callback);
	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
	{
		std::cout << "Failed to initialize GLAD" << std::endl;
		return -1;
	}

	uint32_t vertex_shader = compile_shader(1, VertexShaderSource, GL_VERTEX_SHADER);
	uint32_t fragment_shader = compile_shader(1, FragmentShaderSource, GL_FRAGMENT_SHADER);

	uint32_t shader_program = glCreateProgram();
	glAttachShader(shader_program, vertex_shader);
	glAttachShader(shader_program, fragment_shader);
	glLinkProgram(shader_program);
	int32_t success;
	char log[512];
	glGetProgramiv(shader_program, GL_LINK_STATUS, &success);
	if (!success) {
		glGetProgramInfoLog(shader_program, 512, nullptr, log);
		std::cout << "LINK SHADER ERROR: " << log << std::endl;
	}
	glDeleteProgram(vertex_shader);
	glDeleteProgram(fragment_shader);

#if 0
	float vertices[] = {
		-0.5f, -0.5f, 0.0f,
		0.5f, -0.5f, 0.0f,
		0.0f, 0.5f, 0.0f
	};
	uint32_t VBO, VAO;
	glGenVertexArrays(1, &VAO);
	glGenBuffers(1, &VBO);
	glBindVertexArray(VAO);

	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
	glEnableVertexAttribArray(0);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindVertexArray(0);
#endif

	float vertices[] = {
		0.5f, 0.5f, 0.0f,
		-0.5f, 0.5f, 0.0f,
		-0.5f, -0.5f, 0.0f,
		0.5f, -0.5f, 0.0f
	};

	uint32_t indices[] = {
		0, 1, 2,
		1, 2, 3
	};

	uint32_t VBO, VAO;
	glGenVertexArrays(1, &VAO);
	glGenBuffers(1, &VBO);
	glBindVertexArray(VAO);

	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	uint32_t EBO;
	glGenBuffers(1, &EBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
	glEnableVertexAttribArray(0);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindVertexArray(0);

	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

	while (!glfwWindowShouldClose(win)) {
		handle_event(win);

		glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		glUseProgram(shader_program);
		glBindVertexArray(VAO);
		// glDrawArrays(GL_TRIANGLES, 0, 3);
		glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

		glfwSwapBuffers(win);
		glfwPollEvents();
	}

	glDeleteVertexArrays(1, &VAO);
	glDeleteBuffers(1, &VBO);
	glDeleteBuffers(1, &EBO);
	glDeleteProgram(shader_program);

	glfwTerminate();

	return 0;
}

